package main

import (
	"log"
	"github.com/gin-gonic/gin"
	"git.oschina.net/zhgk/sqldb"
	"git.oschina.net/zhgk/config"

	"app/midleware"
	"app/router"
)

//初始化数据库
func initDB(mode string) error {
	jdbcUrl, maxOpen, maxIdle, maxLife, err := config.ReadJdbcConfig("conf/jdbc.ini", mode)
	if err != nil {
		return err
	}
	err = sqldb.AddDB("default", "mysql", jdbcUrl, maxOpen, maxIdle, maxLife)
	if err != nil {
		return err
	}
	return nil
}
func getServerAddress(mode string) (lis string, err error) {
	lis, err = config.ReadServerConfig("conf/server.ini", mode)
	return lis, err
}

func main() {
	gin.SetMode(gin.DebugMode)
	//初始化数据库
	err := initDB(gin.Mode())
	if err != nil {
		log.Println(err.Error())
		panic(err.Error())
	}
	//配置日志
	lis, err := getServerAddress(gin.Mode())
	if err != nil {
		log.Println(err.Error())
		panic(err.Error())
	}
	r := gin.New()
	//初始化中间件
	r.Use(gin.Logger())
	r.Use(gin.Recovery())
	//自定义中间件
	r.Use(midleware.CORSMiddleware())
	//截取application/json传输的数据
	r.Use(midleware.BodyToJsonMiddleware())
	//初始化路由
	router.InitRouter(r)
	//配置静态文件目录
	r.Run(lis)
}
